package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.ContactUser;
import org.example.demo.mapper.ContactUserMapper;
import org.example.demo.service.ContactUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 联系人用户关联服务实现类
 */
@Service
public class ContactUserServiceImpl extends ServiceImpl<ContactUserMapper, ContactUser> implements ContactUserService {

    @Override
    public Page<ContactUser> selectPage(ContactUser contactUser, Integer pageNum, Integer pageSize) {
        Page<ContactUser> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        QueryWrapper<ContactUser> queryWrapper = new QueryWrapper<>();

        // 构建查询条件
        queryWrapper.lambda()
                .eq(contactUser.getUserId() != null, ContactUser::getUserId, contactUser.getUserId())
                .eq(contactUser.getContactId() != null, ContactUser::getContactId, contactUser.getContactId());

        return this.page(page, queryWrapper);
    }

    @Override
    public List<Long> getUserIdsByContactId(Long contactId) {
        LambdaQueryWrapper<ContactUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactUser::getContactId, contactId);

        return this.list(queryWrapper).stream()
                .map(ContactUser::getUserId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getContactIdsByUserId(Long userId) {
        LambdaQueryWrapper<ContactUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactUser::getUserId, userId);

        return this.list(queryWrapper).stream()
                .map(ContactUser::getContactId)
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Long contactId, List<Long> userIds) {
        // 先删除该联系人下的所有关联
        LambdaQueryWrapper<ContactUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactUser::getContactId, contactId);
        this.remove(queryWrapper);

        // 批量添加新的关联
        if (userIds != null && !userIds.isEmpty()) {
            List<ContactUser> contactUsers = new ArrayList<>();
            userIds.forEach(userId -> {
                ContactUser contactUser = new ContactUser();
                contactUser.setContactId(contactId);
                contactUser.setUserId(userId);
                contactUsers.add(contactUser);
            });
            return this.saveBatch(contactUsers);
        }
        return true;
    }

    @Override
    public boolean remove(Long contactId, Long userId) {
        LambdaQueryWrapper<ContactUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ContactUser::getContactId, contactId)
                .eq(ContactUser::getUserId, userId);
        return this.remove(queryWrapper);
    }
}